package org.example.gift.controller;

import com.baomidou.mybatisplus.plugins.Page;
import org.example.gift.domain.UserAddress;
import org.example.gift.query.UserAddressQuery;
import org.example.gift.service.IUserAddressService;
import org.example.gift.util.AjaxResult;
import org.example.gift.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userAddress")
public class UserAddressController {
    @Autowired
    public IUserAddressService userAddressService;

    /**
     * 保存和修改公用的
     * @param userAddress  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody UserAddress userAddress){

        if( userAddress.getId()!=null)
            userAddressService.updateById(userAddress);
        else
            userAddressService.insert(userAddress);
        return AjaxResult.success();

    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        userAddressService.deleteById(id);
        return AjaxResult.success();

    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {

        UserAddress userAddress = userAddressService.selectById(id);
        return AjaxResult.success(userAddress);

    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        List< UserAddress> list = userAddressService.selectList(null);
        return AjaxResult.success(list);

    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody UserAddressQuery query)
    {
        Page<UserAddress> page = new Page<UserAddress>(query.getPage(),query.getRows());
        page = userAddressService.selectPage(page);
        PageList pageList = new PageList<UserAddress>(page.getTotal(),page.getRecords());
        return AjaxResult.success(pageList);
    }





}